package com.qcby.controller;

import com.qcby.common.Result;
import com.qcby.entity.TCategory;
import com.qcby.entity.TOrder;
import com.qcby.form.TOrderCreateForm;
import com.qcby.form.TOrderListForm;
import com.qcby.service.TOrderService;
import com.qcby.vo.PageVO;
import com.qcby.vo.TOrderDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2025-02-04
 */
@RestController
@RequestMapping("/order")
public class TOrderController {
    @Autowired
    private TOrderService orderService;

    /**
     * 查询订单
     * @param orderListForm
     * @return
     */
    @GetMapping("/list")
    public Result<PageVO> list(@Valid TOrderListForm orderListForm) {
        PageVO pageVO = orderService.orderList(orderListForm);
        return Result.success(pageVO);
    }

    /**
     * 更新订单状态
     * 批量发货
     */
    @PostMapping("/update")
    public Result info(@RequestBody Integer[] ids){
        List<TOrder> orders=new ArrayList<>();
        for(Integer id:ids){
            TOrder order=orderService.getById(id);
            order.setOrderStatus("40");
            orders.add(order);
        }
        boolean result=orderService.updateBatchById(orders);
        if(!result) return Result.error("发货失败");
        return Result.ok();
    }

    /**
     * 订单详情
     */
    @GetMapping("/{id}")
    public Result<TOrderDetailVO> detail(@PathVariable("id") Integer id) {
        TOrderDetailVO detail = orderService.getOrderDetail(id);
        return Result.success(detail);
    }

    /**
     * 创建订单
     */
    @PostMapping
    public Result create(@RequestBody @Valid TOrderCreateForm form) {
        orderService.createOrder(form);
        return Result.success();
    }

    /**
     * 支付订单
     */
    @PutMapping("/{id}/pay")
    public Result pay(@PathVariable("id") Integer id) {
        orderService.payOrder(id);
        return Result.success();
    }

    /**
     * 发货
     */
    @PutMapping("/{id}/deliver")
    public Result deliver(@PathVariable("id") Integer id) {
        orderService.deliverOrder(id);
        return Result.success();
    }

    /**
     * 申请退款
     */
    @PutMapping("/{id}/refund/apply")
    public Result applyRefund(@PathVariable("id") Integer id) {
        orderService.applyRefund(id);
        return Result.success();
    }

    /**
     * 确认退款
     */
    @PutMapping("/{id}/refund/confirm")
    public Result confirmRefund(@PathVariable("id") Integer id) {
        orderService.confirmRefund(id);
        return Result.success();
    }

    /**
     * 标记订单异常
     */
    @PutMapping("/{id}/exception")
    public Result markException(@PathVariable("id") Integer id, @RequestParam String reason) {
        orderService.markOrderException(id, reason);
        return Result.success();
    }
}

